/**
 * Created by guodong on 2017/9/12.
 */
'use strict';
angular.module('app')
    .controller('platInfoController', [ '$rootScope', '$scope', '$http', '$state','$timeout',
        function($rootScope, $scope, $http, $state, $timeout) {
            $scope.title = '资讯平台管理';
            $scope.searchParams = { };
            $scope.loading = false;
            $scope.param={};
            $scope.search = function () {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name+'/platInfoManage/platInfo/list',
                    data: $scope.searchParams
                }).then(function(result) {
                    console.log(result);
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.searchRe = function () {
                $scope.loading = true;
                $scope.searchParams.pageNum = 1;
                $.ajax({
                    url : $scope.app.project_name+'/platInfoManage/platInfo/list',
                    data: $scope.searchParams
                }).then(function(result) {

                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.pageInfo = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

            $scope.search();

            $scope.clearSearch = function() {
                $scope.searchParams= { };
                $scope.search();
            }

            // 翻页
            $scope.pagination = function (pageNo) {
                $scope.searchParams.pageNum=pageNo;
                $scope.search();
            };

            // 删除
            $scope.deleteByPlatInfo=function (platNo) {
                $scope.alertModel({
                    text:'确定删除？',
                    btnCancel:true,
                    resCode:"10000"
                }).then(function(){
                    $.ajax({
                        url : $scope.app.project_name+'/platInfoManage/platInfo/delete',
                        data: {platNo:platNo}
                    }).then(function(result) {
                        $scope.loading = false;

                        if (result.httpCode == 200) {
                            $scope.alertModal({"content":"操作成功"});
                        }
                        $scope.search();
                    });

                })

            }

            $scope.changeParent = function () {
                $.ajax({
                    url : $scope.app.project_name+ '/platInfoManage/platInfo/selPlatInfoNameList',
                    data: { }
                }).then(function(result) {
                    if (result.httpCode == 200) {
                        $scope.platInfoListName = result.data;
                    } else {
                        $scope.msg = result.msg;
                    }
                });
                $scope.searchRe();
            }
            $scope.changeParent();

            $scope.oper = function (platNo,$event) {

                $.ajax({
                    url : $scope.app.project_name+'/platInfoManage/platInfo/banEnable',
                    data: {platNo:platNo,enable:$event.target.checked?'Y':'N'}
                }).then(function(result) {
                    $scope.loading = false;

                    if (result.httpCode == 200) {
                        $scope.alertModal({"content":"操作成功"});
                    }
                    $scope.search();
                });
            }
            $scope.isDefaultSet = function (platNo,$event) {
                $.ajax({
                    url : $scope.app.project_name+'/platInfoManage/platInfo/isRecommendPlat',
                    data: {platNo:platNo,isDefault:$event.target.checked?'Y':'N'}
                }).then(function(result) {
                    $scope.loading = false;
                    console.log(result.httpCode);
                    if (result.httpCode == 200) {
                        console.log("=====标记下")
                        $scope.search();
                    }

                });
            }
            $scope.platLogo = [];
            $scope.preview2 = function(platLogo){
                if (platLogo != null && platLogo != '') {
                    $scope.platLogo = platLogo.split('|');
                }
                $scope.firstApply = true;
            }

            // 平台名称鼠标移入的动作
            $scope.introduction = [];
            $scope.preview = function(introduction){
                var showDiv = document.getElementById('showDiv');
                showDiv.style.left = event.clientX;
                showDiv.style.top = event.clientY;
                showDiv.style.display = 'block';
                showDiv.innerHTML = introduction;
            }
            // 平台名称鼠标移出的动作
            $scope.outHide = function(){
                var showDiv = document.getElementById('showDiv');
                showDiv.style.display = 'none';
                showDiv.innerHTML = '';
            }

            // 平台简介鼠标移入的动作
            $scope.introduction = [];
            $scope.preview1 = function(introduction){
                var showDiv1 = document.getElementById('showDiv1');
                showDiv1.style.left = event.clientX;
                showDiv1.style.top = event.clientY;
                showDiv1.style.display = 'block';
                showDiv1.innerHTML = introduction;
            }
            // 平台简介鼠标移出的动作
            $scope.outHide1 = function(){
                var showDiv1 = document.getElementById('showDiv1');
                showDiv1.style.display = 'none';
                showDiv1.innerHTML = '';
            }

            // 拖拽排序
            $scope.editSort=function ($event) {
                if($('#platNo').val()){
                    $scope.alertModal({'content':'平台编号为空方可拖拽排序'});
                    return;
                }
                if(!$('#moduleNo').val()){
                    $scope.alertModal({'content':'分类名称不能为空'});
                    return;
                }
                if($('#platName').val()){
                    $scope.alertModal({'content':'平台名称为空方可拖拽排序'});
                    return;
                }

                $("#platInfoList").sortable({opacity: 0.5,//拖动的透明度
                    revert: true, //缓冲效果
                    cursor: 'move', //拖动的时候鼠标样式
                    connectWith: ".column", scrollSpeed: 40});
                $("#platInfoList").disableSelection();

                for (var i = 0; i < $scope.pageInfo.records.length; i++) {
                    $(".editSortBtn").hide();
                    $("#sortId").hide();
                    $(".saveSortBtn").show();
                }
            }

            $scope.saveSort=function ($event) {
                var batchUpdates = new Array();//声明一个数组来保存多个的id值
                var sortArray = new Array();//声明一个数组来保存多个序号的值
                for (var j = 0; j < $scope.pageInfo.records.length; j++) {
                    var tr = $(".sort  tbody tr")[j];
                    var id = $($(tr).find("td")[1]).html();
                    var sort = j + 1;
                    sortArray[j] = sort;//用来存放序号
                    var batchUpdate = id + "-" + sort;
                    // alert(batchUpdate);
                    batchUpdates[j] = batchUpdate;
                }
                $.ajax({
                    url: $scope.app.project_name + '/platInfoManage/platInfo/editSort',
                    data: {'ids': batchUpdates},
                    traditional: true
                }).then(function (result) {
                    if (result.httpCode == 200) {
                        $scope.alertModal({"content": "操作成功"});
                        $(".editSortBtn").show();
                        $("#sortId").show();
                        $(".saveSortBtn").hide();
                        $("#platInfoList").sortable('destroy');//解除拖拽
                        $timeout(function () {
                            $scope.search();
                        }, 1000);
                    } else {
                        $scope.msg = result.msg;
                    }
                });
            }

        } ]);